package Test;

import javax.print.attribute.SetOfIntegerSyntax;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @leiwenkang 2017-12-29 17:54.
 */
public class SeriesLength {
    public static void main(String[] args) {
        String line = "8,9,4,5,1,2,7,6,3";
        System.out.println(getSeriesLength(line));
    }

    public static String getSeriesLength(String line) {
        if (line == null || line.length() == 0) {
            return null;
        }
        String[] str = line.split(",");
        //已计算的数字value设为true，防止重复计算
        Map<Integer, Boolean> map = new HashMap<>();
        for (int i = 0; i < str.length; i++) {
            int val = Integer.parseInt(str[i]);
            map.put(val, false);
        }
        int max = 0;
        for (int i = 0; i < str.length; i++) {
            int val = Integer.parseInt(str[i]);
            if (map.get(val)) {
                continue;
            }
            map.put(val, true);
            int value = 1;
            int next = val + 1;
            int before = val - 1;
            while (map.get(next) != null) {
                map.put(val, true);
                value = value + 1;
                next++;
            }
            while (map.get(before) != null) {
                map.put(before, true);
                value = value + 1;
                before--;
            }
            if (value > max) {
                max = value;
            }
        }
        return max + "";
    }

}
